<?php
// $Id: restorebulk.php, v 1.0.0.0 2008/07/17 deanthayer Exp $
//based on: restore.php,v 1.40.6.2 2007/02/14 20:59:14 skodak Exp $
//This script is used to execute the bulk restore proccess.

//Define some globals for all the script
//define('DEBUG_MODE', true);
if (!defined('DEBUG_MODE')) {
	define('RESTORE_SILENTLY', true);
}

//Units used
require_once ("../config.php");
require_once ("../lib/xmlize.php");
require_once ("../course/lib.php");
require_once ("lib.php");
require_once ("restorelib.php");
require_once ("bb/restore_bb.php");
require_once ("$CFG->libdir/blocklib.php");
require_once ("$CFG->libdir/wiki_to_markdown.php");
require_once ("$CFG->libdir/adminlib.php");

//Optional
$id = required_param('id');
$archive_term_cat = required_param('termcat');

//Check login
require_login();

if (!empty ($id)) {
	if (!has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE, $id))) {
		if (empty ($to)) {
			error("You need to be a teacher or admin user to use this page.", "$CFG->wwwroot/login/index.php");
		} else {
			if (!has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE, $to))) {
				error("You need to be a teacher or admin user to use this page.", "$CFG->wwwroot/login/index.php");
			}
		}
	}
} else {
	if (!has_capability('moodle/site:restore', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
		error("You need to be an admin user to use this page.", "$CFG->wwwroot/login/index.php");
	}
}

//Check site
if (!$site = get_site()) {
	error("Site not found!");
}

//Print header
if (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
	print_header("Bulk Course Restore", 'Bulk Course Restore', "Bulk Course Restore -> Choose Source Course and Destination Categories");
}

//Check necessary functions exists. Thanks to gregb@crowncollege.edu
backup_required_functions();

$backupfiles = get_directory_list("$CFG->dataroot/$id");
foreach ($backupfiles as $file) {
	$backup_unique_code = time();
	print "<h3>Restoring $file...</h3>";

	$status = restore_precheck($id, $id . '/' . $file, $errorstr, true);
	if (defined('DEBUG_MODE')) {
		print "status = " . $status . "<br>"; //DEBUG
		print "unique = " . $backup_unique_code . "<br>"; //DEBUG
	}
	$xml_file = $CFG->dataroot . "/temp/backup/" . $backup_unique_code . "/moodle.xml";
	$info = restore_read_xml_info($xml_file);
	$course_header = restore_read_xml_course_header($xml_file);

	//Get and check course
	if (!$course = get_record("course", "id", $id)) {
		error("Course ID was incorrect (can't find it)");
	}

	//Checks for the required files/functions to restore every mod
	$count = 0;
	if ($allmods = get_records("modules")) {
		foreach ($allmods as $mod) {
			$modname = $mod->name;
			$modfile = "$CFG->dirroot/mod/$modname/restorelib.php";
			$modrestore = $modname . "_restore_mods";
			if (file_exists($modfile)) {
				include_once ($modfile);
				if (function_exists($modrestore)) {
					$var = "exists_" . $modname;
					$$var = true;
					$count++;
				}
			}
			//Check module info
			$var = "restore_" . $modname;
			if (isset ($info->mods[$modname]) && $info->mods[$modname]->backup == "true") {
				$$var = 1;
			}
			if (!isset ($$var)) {
				$$var = 0;
			}
			//Check include user info
			$var = "restore_user_info_" . $modname;
			if (isset ($info->mods[$modname]) && $info->mods[$modname]->userinfo == "true") {
				$$var = 1;
			}
			if (!isset ($$var)) {
				$$var = 0;
			}
		}
	}

	//Set other restore parameters
	$restore_metacourse = 0;
	$restore_users = 1;
	$restore_logs = 0;
	$restore_user_files = 1;
	$restore_course_files = 1;
	$restore_messages = 1;
	$restore_restoreto = 2;

	if (!isset ($archive_term_cat)) {
		$course_header->category->id = $archive_term_cat;
	} else {
		$course_header->category->id = 0;
	}
	if ($count == 0) {
		notice("No restorable modules are installed!");
	}

	//Create the restore object and put it in the session
	$restore->backup_unique_code = $backup_unique_code;
	$restore->file = $file;
	if ($allmods = get_records("modules")) {
		foreach ($allmods as $mod) {
			$modname = $mod->name;
			$var = "restore_" . $modname;
			$restore->mods[$modname]->restore = $$var;
			$var = "restore_user_info_" . $modname;
			$restore->mods[$modname]->userinfo = $$var;
			$instances = !empty ($info->mods[$mod->name]->instances) ? $info->mods[$mod->name]->instances : NULL;
			if ($instances === NULL) {
				continue;
			}
			foreach ($instances as $instance) {
				$var = 'restore_' . $modname . '_instance_' . $instance->id;
				$$var = 1;
				$restore->mods[$modname]->instances[$instance->id]->restore = $$var;
				$var = 'restore_user_info_' . $modname . '_instance_' . $instance->id;
				$$var = 1;
				$restore->mods[$modname]->instances[$instance->id]->userinfo = $$var;
			}
		}
	}
	$restore->restore_restorecatto = $archive_term_cat; // New category
	$restore->restoreto = $restore_restoreto;
	$restore->metacourse = $restore_metacourse;
	$restore->users = $restore_users;
	$restore->logs = $restore_logs;
	$restore->user_files = $restore_user_files;
	$restore->course_files = $restore_course_files;
	$restore->messages = $restore_messages;
	$restore->course_id = 0;
	$restore->rolesmapping[0] = 0;
	$restore->rolesmapping[1] = 1;
	$restore->rolesmapping[2] = 2;
	$restore->rolesmapping[3] = 3;
	$restore->rolesmapping[4] = 4;
	$restore->rolesmapping[5] = 5;

	// Prepare the SESSION for restore
	if (defined('DEBUG_MODE')) {
		print "status = $status <br>";
	}
	if ($status) {
		$SESSION->info = $info;
		$SESSION->course_header = $course_header;
		$SESSION->restore = $restore;
	}
	if (defined('DEBUG_MODE')) {
		print "<h3>SESSION</h3>";
		print "<pre>";
		print_r($SESSION);
		print "</pre>";
	}

	//Final access control check
	if ($restore->course_id == 0 and !user_can_create_courses()) {
		error("You need to be a creator or admin to restore into new course!");
	} else
		if ($restore->course_id != 0 and !has_capability('moodle/site:backup', get_context_instance(CONTEXT_COURSE, $restore->course_id))) {
			error("You need to be an edit teacher or admin to restore into selected course!");
		}

	//Prevent multiple restore executions...
	if (empty ($SESSION->cancontinue)) {
		// error("Multiple restore execution not allowed!");
	}
	//Unset this for the future
	unset ($SESSION->cancontinue);
	$errstr = '';
	restore_execute($restore, $info, $course_header, $errstr);
}
print_simple_box_end();

//Print footer 
print_footer();